package com.example.softwareconfigurationcoursedesign.LoginRegister;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DBHelper {
    private static final String DB_CONN_RUL = "jdbc:mysql://localhost:3306/course_design?useSSL=false";
    private static final String DB_USER_NAME = "root";
    private static final String DB_PASSWORD = "123456";

    public DBHelper() {
        // DBHelper类的构造函数，这里为空
    }

    public static Connection getConnection() {
        Connection connection = null;

        try {
            Class.forName("com.mysql.jdbc.Driver");  // 加载MySQL数据库驱动
            connection = DriverManager.getConnection(DB_CONN_RUL, DB_USER_NAME, DB_PASSWORD);  // 获取数据库连接
        } catch (SQLException | ClassNotFoundException var2) {
            var2.printStackTrace(System.err);  // 打印异常信息
        }

        return connection;  // 返回数据库连接
    }

    public static void closeAll(Connection connection, PreparedStatement pstmt, ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();  // 关闭结果集
            } catch (Exception var6) {
                var6.printStackTrace(System.err);  // 打印异常信息
            }
        }

        if (pstmt != null) {
            try {
                pstmt.close();  // 关闭PreparedStatement
            } catch (Exception var5) {
                var5.printStackTrace(System.err);  // 打印异常信息
            }
        }

        if (connection != null) {
            try {
                connection.close();  // 关闭数据库连接
            } catch (Exception var4) {
                var4.printStackTrace(System.err);  // 打印异常信息
            }
        }
    }
}
